package com.example.a2306_fx.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2306_fx.R
import com.example.a2306_fx.databinding.FragmentCategoryBinding
import com.example.a2306_shop.adapter.OneAdapter
import com.example.a2306_shop.adapter.ProductAdapter
import com.example.a2306_shop.adapter.TwoAdapter
import com.example.base.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var productAdapter: ProductAdapter

    var cid = 0
    var page = 1
    var limit = 3
    override fun getLayout(): Int = R.layout.fragment_category

    override fun getViewModel(): Class<CategoryViewModel>  = CategoryViewModel::class.java

    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,4)

        productAdapter = ProductAdapter()
        binding.rvProduct.adapter = productAdapter
        binding.rvProduct.layoutManager = GridLayoutManager(context,2)

        //请求分类
        viewModel.category()
        //点击一级分类联动
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            //二级分类变化   三级分类重新请求
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            page = 1
            cid = twoAdapter.data[0].id
            viewModel.product(cid, limit, page)
        }
        //点击二级分类联动
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            //三级分类重新请求
            page = 1
            cid = twoAdapter.data[position].id
            viewModel.product(cid, limit, page)
        }
        //刷新加载更多
        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.product(cid, limit, page)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.product(cid, limit, page)
                binding.smart.finishLoadMore()
            }
        })
        //点击商品列表，跳转到详情界面
        productAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("pid",productAdapter.data[position].id).navigation()
        }
    }

    override fun initData() {
        //观察分类结果
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                //一级列表显示
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级列表显示   注意显示的是第一个的child
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()
                //请求三级分类   注意 cid  是 二级列表的第一个
                cid = twoAdapter.data[0].id
                page = 1
                viewModel.product(cid, limit, page)
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.productSuccess.observe(this){
            if(it.code==200) {
                if (page == 1) {
                    productAdapter.data.clear()
                }
                productAdapter.data.addAll(it.data.list)
                productAdapter.notifyDataSetChanged()
                if(it.data.list.size==0){
                    ToastUtils.showLong("没有更多数据了")
                }
            }
        }

    }

}